-- Keybindings
-- map helper
local function map(mode, lhs, rhs, opts)
    local options = {
        noremap = true
    }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end
local function xmap(lhs, rhs, opts)
    local options = {
        noremap = true
    }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap('x', lhs, rhs, options)
end
local function nmap(lhs, rhs, opts)
    local options = {
        noremap = true
    }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap('n', lhs, rhs, options)
end
local function vmap(lhs, rhs, opts)
    local options = {
        noremap = true
    }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap('v', lhs, rhs, options)
end
local function imap(lhs, rhs, opts)
    local options = {
        noremap = true
    }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap('i', lhs, rhs, options)
end
local function omap(lhs, rhs, opts)
    local options = {
        noremap = true
    }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    vim.api.nvim_set_keymap('o', lhs, rhs, options)
end

nmap('<esc>', ':noh<cr>', {
    silent = true
});

nmap('<leader>d', ':bd<cr>', {
    silent = true
}) -- delete buffer

-- open file under cursor
nmap('<F8>', ':vertical wincmd f<CR>');
imap('<F8>', ':vertical wincmd f<CR>');

-- nvim-tree
nmap('<leader>tg', [[:NvimTreeToggle<cr>]], {})
nmap('<leader>tf', [[:NvimTreeFocus<cr>]], {})
nmap('<leader>tr', [[:NvimTreeFindFile<cr>]], {})

-- bufferline settings
-- https://github.com/akinsho/bufferline.nvim
map('n', '<leader>1', '<Cmd>BufferLineGoToBuffer 1<CR>')
map('n', '<leader>2', '<Cmd>BufferLineGoToBuffer 2<CR>')
map('n', '<leader>3', '<Cmd>BufferLineGoToBuffer 3<CR>')
map('n', '<leader>4', '<Cmd>BufferLineGoToBuffer 4<CR>')
map('n', '<leader>5', '<Cmd>BufferLineGoToBuffer 5<CR>')
map('n', '<leader>6', '<Cmd>BufferLineGoToBuffer 6<CR>')
map('n', '<leader>7', '<Cmd>BufferLineGoToBuffer 7<CR>')
map('n', '<leader>8', '<Cmd>BufferLineGoToBuffer 8<CR>')
map('n', '<leader>9', '<Cmd>BufferLineGoToBuffer 9<CR>')
map('n', '<leader>-', '<Cmd>BufferLineCycleNext<CR>')
map('n', '<leader>+', '<Cmd>BufferLineCyclePrev<CR>')
nmap('<F1>', ':BufferLineCyclePrev<CR>', {
    silent = true
}) -- buffer prev
nmap('<F2>', ':BufferLineCycleNext<CR>', {
    silent = true
}) -- buffer next

-- telescope
local builtin = require('telescope.builtin')
vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
vim.keymap.set('n', '<leader>fg', builtin.live_grep, {})
vim.keymap.set('n', '<leader>fb', builtin.buffers, {})
vim.keymap.set('n', '<leader>fh', builtin.help_tags, {})
vim.keymap.set("n", "<space>e", "<Cmd>Telescope frecency workspace=CWD<CR>")

-- vim-easy-align
-- Start interactive EasyAlign in visual mode (e.g. vipga)
map('x', 'al', '<Plug>(EasyAlign)')
-- Start interactive EasyAlign for a motion/text object (e.g. gaip)
map('n', 'al', '<Plug>(EasyAlign)')

-- TODO-COMMENTS --
vim.keymap.set("n", "]t", function()
    require("todo-comments").jump_next()
end, {
    desc = "Next todo comment"
})

vim.keymap.set("n", "[t", function()
    require("todo-comments").jump_prev()
end, {
    desc = "Previous todo comment"
})


